{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典基础\n",
    "- 建立一个简单的字典并打印。\n",
    "    1. 字典是一系列的**键-值**对用“{}”包围，可以使用键去访问与之相关联的值，**键-值**对之间用“,”隔开，值可以是任意的Python对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "green\n",
      "5\n",
      "you got 5 points!\n"
     ]
    }
   ],
   "source": [
    "aline_0 = {'color': 'green', 'points': 5}\n",
    "new_point = aline_0['points']\n",
    "print(aline_0['color'])\n",
    "print(aline_0['points'])\n",
    "print(\"you got \" + str(new_point) + \" points!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    2.添加键－值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5} \n",
      "\n",
      "{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25} \n",
      "\n",
      "{'color': 'blue', 'points': '6'} \n",
      "\n"
     ]
    }
   ],
   "source": [
    "aline_0 = {'color': 'green', 'points': 5}\n",
    "print(aline_0,\"\\n\")\n",
    "aline_0['x_position'] = 0\n",
    "aline_0['y_position'] = 25\n",
    "print(aline_0,\"\\n\")\n",
    "aline_1 = {}\n",
    "aline_1['color'] = 'blue'\n",
    "aline_1['points'] = '6'\n",
    "print(aline_1,\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    3. 修改字典中的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The aline is green.\n",
      "The aline is blue.\n",
      "原始位置为： 0\n",
      "新位置为： 2\n",
      "新位置为： 4\n"
     ]
    }
   ],
   "source": [
    "aline_0 = {'color': 'green', 'points': 5}\n",
    "print(\"The aline is \" + aline_0['color'] + \".\")\n",
    "aline_0['color'] = 'blue'\n",
    "print(\"The aline is \" + aline_0['color'] + \".\")\n",
    "\n",
    "aline_1 = {'x_position': 0, 'y_position': 25, 'speed': 'medium'}\n",
    "print(\"原始位置为： \" + str(aline_1['x_position']))\n",
    "\n",
    "if aline_1['speed'] == 'slow':\n",
    "    x_increment = 1\n",
    "elif aline_1['speed'] == 'medium':\n",
    "    x_increment = 2\n",
    "else:\n",
    "    x_increment = 3\n",
    "#新位置 = 旧位置 + 增量\n",
    "aline_1['x_position'] = aline_1['x_position'] + x_increment\n",
    "print(\"新位置为： \" + str(aline_1['x_position']))\n",
    "aline_1['speed'] = 'fast'\n",
    "aline_1['x_position'] = aline_1['x_position'] + x_increment\n",
    "print(\"新位置为： \" + str(aline_1['x_position']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    4.删除键-对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5}\n",
      "{'points': 5}\n"
     ]
    }
   ],
   "source": [
    "aline_0 = {'color': 'green', 'points': 5}\n",
    "print(aline_0)\n",
    "del aline_0['color']\n",
    "print(aline_0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    5.由类似对象组成的字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sara is favorite languages is Python.\n"
     ]
    }
   ],
   "source": [
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "print(\"sara is favorite languages is \" + favorite_languages['sara'].title() + \".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 遍历字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.遍历所有的键-值对.`dict.items()` for循环遍历之后会返回键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Key: username\n",
      "Value: efermi\n",
      "\n",
      "Key: first\n",
      "Value: enrico\n",
      "\n",
      "Key: last\n",
      "Value: fermi\n",
      "\n",
      "\n",
      "Jen is favorite language is C .\n",
      "Sara is favorite language is Python.\n",
      "Edwar is favorite language is Ruby.\n",
      "Phil is favorite language is Python.\n"
     ]
    }
   ],
   "source": [
    "user_0 = {\n",
    "    'username': 'efermi',\n",
    "    'first': 'enrico',\n",
    "    'last': 'fermi'\n",
    "}\n",
    "for key, value in user_0.items():\n",
    "    print(\"\\nKey: \" + key)\n",
    "    print(\"Value: \" + value)\n",
    "    \n",
    "print(\"\\n\")\n",
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "for name, language in favorite_languages.items():\n",
    "    print(name.title() + \" is favorite language is \" + language.title() + \".\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.遍历所有的键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jen\n",
      "Sara\n",
      "Edwar\n",
      "Phil\n",
      "\n",
      "\n",
      "Hi Jen,I see you favorite language is C !\n",
      "Hi Sara,I see you favorite language is Python!\n",
      "C \n",
      "eric is not blong it.\n"
     ]
    }
   ],
   "source": [
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "for name in favorite_languages.keys():\n",
    "    print(name.title())\n",
    "print(\"\\n\")\n",
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "friends = ['jen', 'sara']\n",
    "for name in friends:\n",
    "    print(\"Hi \" + name.title() + \",\" + \"I see you favorite language is \" \n",
    "          +  favorite_languages[name].title() + \"!\")\n",
    "#dict.keys()也可以判断某个键是否属于这个字典\n",
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "if 'jen' in favorite_languages.keys():\n",
    "    print(favorite_languages['jen'])\n",
    "if 'eric' not in favorite_languages.keys():\n",
    "    print(\"eric is not blong it.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3-1.按顺序遍历字典的所有键,`sorted()` 是按照字母从小到大的顺序排列键，`revese=True` 顺序翻转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Edwar, thank you love study.\n",
      "Jen, thank you love study.\n",
      "Phil, thank you love study.\n",
      "Sara, thank you love study.\n"
     ]
    }
   ],
   "source": [
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "for name in sorted(favorite_languages.keys()):\n",
    "    print(name.title() + \", thank you love study.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3-2.遍历字典中的所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下面的字典被提到： \n",
      "C \n",
      "Python\n",
      "Ruby\n",
      "Python\n"
     ]
    }
   ],
   "source": [
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'Python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "print(\"下面的字典被提到： \")\n",
    "for value in favorite_languages.values():\n",
    "    print(value.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3-3 `set()` 函数了解一下！set是一个集合{}，里面的值不允许重复**区分大小写**，可以用来做数据剔除，比如：\n",
    "```\n",
    "    list_a = [1,2,3,1,3,2,1,9]\n",
    "    list_b = set(list_b)\n",
    "    print(list_b)\n",
    "    {1, 2, 3, 9}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 9}\n",
      "下面的字典被提到： \n",
      "Ruby\n",
      "Python\n",
      "C \n"
     ]
    }
   ],
   "source": [
    "list_a = [1,2,3,1,3,2,1,9]\n",
    "list_b = set(list_a)\n",
    "print(list_b)\n",
    "favorite_languages = {\n",
    "    'jen': 'C ',\n",
    "    'sara': 'python',\n",
    "    'edwar': 'ruby',\n",
    "    'phil': 'python'\n",
    "}\n",
    "print(\"下面的字典被提到： \")\n",
    "for value in set(favorite_languages.values()):\n",
    "    print(value.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.嵌套\n",
    "4.1列表中存储字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'yellow', 'points': 10}\n",
      "{'color': 'red', 'points': 15}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "...\n",
      "总共创建了 30 个外星人\n"
     ]
    }
   ],
   "source": [
    "aline_0 = {'color': 'green', 'points': 5}\n",
    "aline_1 = {'color': 'yellow', 'points': 10}\n",
    "aline_2 = {'color': 'red', 'points': 15}\n",
    "\n",
    "alines = [aline_0, aline_1, aline_2]\n",
    "for aline in alines:\n",
    "    print(aline)\n",
    "alines = []\n",
    "for aline_number in range(30):\n",
    "    new_aline = {'color': 'green', 'points': 5, 'speed': 'slow'}\n",
    "    alines.append(new_aline)\n",
    "for aline in alines[1:6]:\n",
    "    print(aline)\n",
    "print(\"...\")\n",
    "print(\"总共创建了 %s 个外星人\" %str(len(alines)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "{'color': 'green', 'points': 5, 'speed': 'slow'}\n",
      "...\n",
      "总共创建了 30 个外星人\n",
      "{'color': 'red', 'points': 15, 'speed': 'fast'}\n",
      "{'color': 'yellow', 'points': 10, 'speed': 'medium'}\n",
      "{'color': 'yellow', 'points': 10, 'speed': 'medium'}\n",
      "{'color': 'red', 'points': 15, 'speed': 'fast'}\n",
      "{'color': 'red', 'points': 15, 'speed': 'fast'}\n"
     ]
    }
   ],
   "source": [
    "alines = []\n",
    "for aline_number in range(30):\n",
    "    new_aline = {'color': 'green', 'points': 5, 'speed': 'slow'}\n",
    "    alines.append(new_aline)\n",
    "for aline in alines[1:6]:\n",
    "    print(aline)\n",
    "print(\"...\")\n",
    "print(\"总共创建了 %s 个外星人\" %str(len(alines)))\n",
    "for aline in alines[1:3]:\n",
    "    if aline['color'] == 'green':\n",
    "        aline['color'] = 'yellow'\n",
    "        aline['points'] = 10\n",
    "        aline['speed'] = 'medium'\n",
    "for aline in alines[:5]:\n",
    "    if aline['color'] == 'green':\n",
    "        aline['color'] = 'red'\n",
    "        aline['points'] = 15\n",
    "        aline['speed'] = 'fast'\n",
    "for aline in alines[0:5]:\n",
    "    print(aline)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.2字典中存储列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你订购了一个 thick-crust 披萨配方如下：\n",
      "\tmushroom\n",
      "\textra cheese\n"
     ]
    }
   ],
   "source": [
    "pizza = {\n",
    "    'crust': 'thick',\n",
    "    'toppings': ['mushroom', 'extra cheese']\n",
    "}\n",
    "print(\"你订购了一个 \" + pizza['crust'] + \"-crust 披萨\" + \"配方如下：\")\n",
    "for topping in pizza['toppings']:\n",
    "    print(\"\\t\" + topping)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Jenis favorite language are:\n",
      "\tC\n",
      "\tJava\n",
      "\n",
      "Sarais favorite language is:\n",
      "\tC++\n",
      "\n",
      "Edwaris favorite language are:\n",
      "\tRuby\n",
      "\tGo\n",
      "\n",
      "Philis favorite language are:\n",
      "\tPython\n",
      "\tHaskell\n"
     ]
    }
   ],
   "source": [
    "favorite_languages = {\n",
    "    'jen': ['c', 'java'],\n",
    "    'sara': ['c++'],\n",
    "    'edwar': ['ruby', 'go'],\n",
    "    'phil': ['python', 'haskell']\n",
    "}\n",
    "for name, languages in favorite_languages.items():\n",
    "    if len(languages) <2:\n",
    "        print(\"\\n\" + name.title() + \"is favorite language is:\")\n",
    "    else:\n",
    "        print(\"\\n\" + name.title() + \"is favorite language are:\")\n",
    "    for language in languages:\n",
    "        print(\"\\t\" + language.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.3字典之中存储字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "usernameaeinstein\n",
      "\tFull_name:Alberteinstein\n",
      "\tLocation: Princeton\n",
      "\n",
      "usernamemcurie\n",
      "\tFull_name:Mariecurie\n",
      "\tLocation: Paris\n"
     ]
    }
   ],
   "source": [
    "user = {\n",
    "    'aeinstein':{\n",
    "        'first': 'albert',\n",
    "        'last': 'einstein',\n",
    "        'location': 'princeton',\n",
    "    },\n",
    "    \n",
    "    'mcurie':{\n",
    "        'first': 'marie',\n",
    "        'last': 'curie',\n",
    "        'location': 'paris',\n",
    "    },\n",
    "}\n",
    "\n",
    "for username, user_info in user.items():\n",
    "    print(\"\\nusername\" + username)\n",
    "    full_name = user_info['first'] + \"\" + user_info['last']\n",
    "    location = user_info['location']\n",
    "    \n",
    "    print(\"\\tFull_name:\" + full_name.title())\n",
    "    print(\"\\tLocation: \" + location.title())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
